home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / Thunderbird Setup 1.0.exe / mail.xpi / bin / components / offlineStartup.js < prev    next >
Encoding:
Text File  |  2004-12-06  |  8.9 KB  |  272 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is the Offline Startup Handler
  16.  *
  17.  *
  18.  * Contributor(s):
  19.  *  David Bienvenu <bienvenu@nventure.com> (Original Author) 
  20.  *
  21.  * Alternatively, the contents of this file may be used under the terms of
  22.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  23.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  24.  * in which case the provisions of the GPL or the LGPL are applicable instead
  25.  * of those above. If you wish to allow use of your version of this file only
  26.  * under the terms of either the GPL or the LGPL, and not to allow others to
  27.  * use your version of this file under the terms of the MPL, indicate your
  28.  * decision by deleting the provisions above and replace them with the notice
  29.  * and other provisions required by the GPL or the LGPL. If you do not delete
  30.  * the provisions above, a recipient may use your version of this file under
  31.  * the terms of any one of the MPL, the GPL or the LGPL.
  32.  *
  33.  * ***** END LICENSE BLOCK ***** */
  34. const kDebug               = false;
  35. const kBundleURI         = "chrome://messenger/locale/offlineStartup.properties";
  36. const kOfflineStartupPref = "offline.startup_state";
  37. var gShuttingDown = false;
  38. var gOfflineStartupMode; //0 = remember last state, 1 = ask me, 2 == online, 3 == offline
  39. const kRememberLastState = 0;
  40. const kAskForOnlineState = 1;
  41. ////////////////////////////////////////////////////////////////////////
  42. // 
  43. //   nsOfflineStartup : nsIProfileStartupListener, nsIObserver
  44. //
  45. //   Check if the user has set the pref to be prompted for
  46. //   online/offline startup mode. If so, prompt the user. Also,
  47. //   check if the user wants to remember their offline state
  48. //   the next time they start up.
  49. //
  50. ////////////////////////////////////////////////////////////////////////
  51.  
  52. var nsOfflineStartup = 
  53. {
  54.   onProfileStartup: function()
  55.   {
  56.     debug("onProfileStartup");
  57.  
  58.       var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  59.         getService(Components.interfaces.nsIPrefBranch);
  60.       var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  61.   
  62.       gOfflineStartupMode = prefs.getIntPref(kOfflineStartupPref);
  63.  
  64.     if (gOfflineStartupMode == kRememberLastState)
  65.     {    
  66.       var offline = !prefs.getBoolPref("network.online");
  67.       // if the user checked "work offline" in the profile mgr UI
  68.       // and forced us offline, remember that in prefs
  69.       // if checked, the "work offline" checkbox overrides our 
  70.       // persisted state
  71.       if (ioService.offline)
  72.         prefs.setBoolPref("network.online", false);
  73.        else
  74.        {
  75.          // if user did not check "work offline" in the profile manager UI
  76.          // use the persisted online state pref to restore our offline state
  77.       ioService.offline = offline;
  78.        }
  79.  
  80.       var observerService = Components.
  81.         classes["@mozilla.org/observer-service;1"].
  82.         getService(Components.interfaces.nsIObserverService);
  83.       observerService.addObserver(this, "network:offline-status-changed", false);
  84.       observerService.addObserver(this, "quit-application", false);
  85.       observerService.addObserver(this, "xpcom-shutdown", false);
  86.     }
  87.     else if (gOfflineStartupMode == kAskForOnlineState)
  88.     {
  89.       var promptService = Components.
  90.         classes["@mozilla.org/embedcomp/prompt-service;1"].
  91.         getService(Components.interfaces.nsIPromptService);
  92.       
  93.       var bundle = getBundle(kBundleURI);
  94.       if (!bundle)
  95.         return;
  96.  
  97.       var title = bundle.GetStringFromName("title");
  98.       var desc = bundle.GetStringFromName("desc");
  99.       var button0Text = bundle.GetStringFromName("workOnline");
  100.       var button1Text = bundle.GetStringFromName("workOffline");
  101.       var checkVal = {value:0};
  102.  
  103.       var result = promptService.confirmEx(null, title, desc, 
  104.         (promptService.BUTTON_POS_0 * promptService.BUTTON_TITLE_IS_STRING) +
  105.         (promptService.BUTTON_POS_1 * promptService.BUTTON_TITLE_IS_STRING),
  106.         button0Text, button1Text, null, null, checkVal);
  107.       debug ("result = " + result + "\n");
  108.       if (result == 1)
  109.         ioService.offline = true;
  110.     }
  111.   },
  112.  
  113.   observe: function(aSubject, aTopic, aData)
  114.   {
  115.     debug("observe: " + aTopic);
  116.  
  117.     if (aTopic == "network:offline-status-changed")
  118.     {
  119.       debug("network:offline-status-changed: " + aData);
  120.       // if we're not shutting down, and startup mode is "remember online state"
  121.       if (!gShuttingDown && gOfflineStartupMode == kRememberLastState)
  122.       {
  123.         debug("remembering offline state: ");
  124.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  125.           getService(Components.interfaces.nsIPrefBranch);
  126.         prefs.setBoolPref("network.online", aData == "online");
  127.       }
  128.  
  129.     }
  130.     else if (aTopic == "app-startup")
  131.     {
  132.       var observerService = Components.
  133.         classes["@mozilla.org/observer-service;1"].
  134.         getService(Components.interfaces.nsIObserverService);
  135.       observerService.addObserver(this, "profile-after-change", false);
  136.     }
  137.     else if (aTopic == "xpcom-shutdown" || aTopic == "quit-application")
  138.     {
  139.       gShuttingDown = true;
  140.     }
  141.     else if (aTopic == "profile-after-change")
  142.     {
  143.       this.onProfileStartup();
  144.     }
  145.   },
  146.  
  147.  
  148.   QueryInterface: function(aIID)
  149.   {
  150.     if (!aIID.equals(Components.interfaces.nsIObserver) &&
  151.         !aIID.equals(Components.interfaces.nsISupports))
  152.       throw Components.results.NS_ERROR_NO_INTERFACE;
  153.  
  154.     return this;
  155.   }
  156. }
  157.  
  158.  
  159. var nsOfflineStartupModule = 
  160. {
  161.   mClassName:     "Offline Startup",
  162.   mContractID:    "@mozilla.org/offline-startup;1",
  163.   mClassID:       Components.ID("3028a3c8-2165-42a4-b878-398da5d32736"),
  164.  
  165.   getClassObject: function(aCompMgr, aCID, aIID)
  166.   {
  167.     if (!aCID.equals(this.mClassID))
  168.       throw Components.results.NS_ERROR_NO_INTERFACE;
  169.     if (!aIID.equals(Components.interfaces.nsIFactory))
  170.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  171.  
  172.     return this.mFactory;
  173.   },
  174.  
  175.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  176.   {
  177.     if (kDebug)
  178.       dump("*** Registering nsOfflineStartupModule (a JavaScript Module)\n");
  179.  
  180.     aCompMgr = aCompMgr.QueryInterface(
  181.                  Components.interfaces.nsIComponentRegistrar);
  182.     aCompMgr.registerFactoryLocation(this.mClassID, this.mClassName, 
  183.       this.mContractID, aFileSpec, aLocation, aType);
  184.  
  185.     // Receive startup notification.
  186.     // We are |getService()|d at app-startup (before profile selection)
  187.     this.getCategoryManager().addCategoryEntry("app-startup", 
  188.       "Offline-startup", "service," + this.mContractID, true, true);
  189.   },
  190.  
  191.   unregisterSelf: function(aCompMgr, aFileSpec, aLocation)
  192.   {
  193.     aCompMgr = aCompMgr.QueryInterface(
  194.                  Components.interfaces.nsIComponentRegistrar);
  195.     aCompMgr.unregisterFactoryLocation(this.mClassID, aFileSpec);
  196.  
  197.     this.getCategoryManager().deleteCategoryEntry("app-startup", 
  198.       "Offline-startup", true);
  199.   },
  200.  
  201.   canUnload: function(aCompMgr)
  202.   {
  203.     return true;
  204.   },
  205.  
  206.   getCategoryManager: function()
  207.   {
  208.     return Components.classes["@mozilla.org/categorymanager;1"].
  209.       getService(Components.interfaces.nsICategoryManager);
  210.   },
  211.  
  212.   //////////////////////////////////////////////////////////////////////
  213.   //
  214.   //   mFactory : nsIFactory
  215.   //
  216.   //////////////////////////////////////////////////////////////////////
  217.   mFactory:
  218.   {
  219.     createInstance: function(aOuter, aIID)
  220.     {
  221.       if (aOuter != null)
  222.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  223.       // return the singleton 
  224.       return nsOfflineStartup.QueryInterface(aIID);
  225.     },
  226.  
  227.     lockFactory: function(aLock)
  228.     {
  229.       // quiten warnings
  230.     }
  231.   }
  232. };
  233.  
  234. function NSGetModule(aCompMgr, aFileSpec)
  235. {
  236.   return nsOfflineStartupModule;
  237. }
  238.  
  239. function getBundle(aURI)
  240. {
  241.   if (!aURI)
  242.     return null;
  243.  
  244.   debug(aURI);
  245.   var bundle = null;
  246.   try
  247.   {
  248.     var strBundleService = Components.
  249.       classes["@mozilla.org/intl/stringbundle;1"].
  250.       getService(Components.interfaces.nsIStringBundleService);
  251.     bundle = strBundleService.createBundle(aURI);
  252.   }
  253.   catch (ex)
  254.   {
  255.     bundle = null;
  256.     debug("Exception getting bundle " + aURI + ": " + ex);
  257.   }
  258.  
  259.   return bundle;
  260. }
  261.  
  262.  
  263. ////////////////////////////////////////////////////////////////////////
  264. //
  265. //   Debug helper
  266. //
  267. ////////////////////////////////////////////////////////////////////////
  268. if (!kDebug)
  269.   debug = function(m) {};
  270. else
  271.   debug = function(m) {dump("\t *** nsOfflineStartup: " + m + "\n");};
  272.